
#import "PrivacyPopView.h"
#import "TTTAttributedLabel.h"
#import "DetailWebController.h"
#import "NSObject+TopViewController.h"
#import "FYDefines.h"
#import "DisclaimerController.h"

@interface PrivacyPopView()<TTTAttributedLabelDelegate>

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) TTTAttributedLabel *contentLabel;
@property (nonatomic,strong) UIView *seperateView;
@property (nonatomic,strong) UIButton *confirmBtn;

@end

@implementation PrivacyPopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self buildUI];
    }
    return self;
}

- (void)buildUI
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    _bgView = [[UIView alloc] init];
    [self addSubview:_bgView];
    [_bgView setBackgroundColor:[UIColor whiteColor]];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(310);
        make.height.mas_equalTo(281);
        make.center.equalTo(self);
    }];
    [_bgView jk_cornerRadius:7 strokeSize:0 color:nil];
    _contentLabel = [TTTAttributedLabel new];
    _contentLabel.delegate = self;
    _contentLabel.lineSpacing = 6.f;
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:15];
    self.contentLabel.activeLinkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor color_5D82FF],(NSString *)kCTForegroundColorAttributeName,nil];
    [_bgView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-70);
    }];
    
    NSString *content = @"尊敬的用户:\n\n您好，为了更好的保障您的个人权益，请务必仔细阅读《用户协议》和《隐私协议》内的所有条款，如果您同意以上协议内容，请点击“我知道了”以继续使用我们的产品和服务，风险早知道将竭力保障您的合法权益与信息安全。";
    
    //设置需要点击的文字的颜色大小
    [_contentLabel setText:content afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

        //得到需要点击的文字的位置
        NSRange selRange = [content rangeOfString:@"《用户协议》"];
        //设定可点击文字的的大小
        UIFont *selFont=[UIFont systemFontOfSize:15];
        CTFontRef selFontRef = CTFontCreateWithName((__bridge CFStringRef)selFont.fontName, selFont.pointSize, NULL);
        //设置可点击文本的大小
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef range:selRange];
        //设置可点击文本的颜色
        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[[UIColor jk_colorWithHexString:@"#0095B6"] CGColor] range:selRange];
        CFRelease(selFontRef);
        //得到需要点击的文字的位置
        NSRange selRange1 = [content rangeOfString:@"《隐私协议》"];

        //设定可点击文字的的大小
        UIFont *selFont1 = [UIFont systemFontOfSize:14];
        CTFontRef selFontRef1 = CTFontCreateWithName((__bridge CFStringRef)selFont1.fontName, selFont1.pointSize, NULL);
        //设置可点击文本的大小
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef1 range:selRange1];
        //设置可点击文本的颜色
        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor jk_colorWithHexString:@"#0095B6"] CGColor] range:selRange1];
        [mutableAttributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleThick) range:selRange1];
        CFRelease(selFontRef1);
        return mutableAttributedString;
    }];
   //设置可点击文字的颜色和去除下划线
    _contentLabel.linkAttributes = @{(NSString *)kCTForegroundColorAttributeName:(id)[[UIColor jk_colorWithHexString:@"#0095B6"] CGColor],
                                      (NSString *)kCTUnderlineStyleAttributeName:[NSNumber numberWithBool:NO]};
    NSRange selRange = [content rangeOfString:@"《用户协议》"];
    [_contentLabel addLinkToTransitInformation:@{@"select":@"《用户协议》"} withRange:selRange];
    
    NSRange selRange1 = [content rangeOfString:@"《隐私协议》"];
    [_contentLabel addLinkToTransitInformation:@{@"select":@"《隐私协议》"} withRange:selRange1];
    
    _seperateView = [[UIView alloc] init];
    [_bgView addSubview:_seperateView];
    [_seperateView setBackgroundColor:[UIColor jk_colorWithHexString:@"#0095B6"]];
    [_seperateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(-51);
    }];
    
    _confirmBtn = [[UIButton alloc] init];
    [_confirmBtn setTitleColor:[UIColor jk_colorWithHexString:@"#0095B6"] forState:UIControlStateNormal];
    [_confirmBtn setTitle:@"我知道了" forState:UIControlStateNormal];
    _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_bgView addSubview:_confirmBtn];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    [_confirmBtn addTarget:self action:@selector(touchConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchConfirmBtn
{
    
//    [FYUserDefault setObject:FY_UTIL.appVersion forKey:FY_privace_version];
//    [FYUserDefault synchronize];
    
    [NSUserDefaults jk_setObject:@(1) forKey:K_PRIVACYPOPSHOW];
    [self dismiss];
}


#pragma TTTAttributedLabel Delegate
//文字的点击事件
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
    NSString *str = [components objectForKey:@"select"];
    
    if ([str isEqualToString:@"《用户协议》"]) {
        DisclaimerController *vc = [[DisclaimerController alloc] init];
        vc.titleStr = @"风险早知道用户协议";
        vc.type = @"serviceStatment";
        vc.popView = self;
        [[self currentController].navigationController pushViewController:vc animated:YES];
    }else if ([str isEqualToString:@"《隐私协议》"]) {
        DetailWebController *vc = [[DetailWebController alloc] init];
        vc.title = @"隐私协议";
        vc.popView = self;
        vc.url = [NSString stringWithFormat:@"%@%@",BASE_URL,profile_privacyAgreement];;
        [[self currentController].navigationController pushViewController:vc animated:YES];
    }
}
@end
